package com.chocus.core.service.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.chocus.core.bean.product.Brand;
import com.chocus.core.bean.product.Product;
import com.chocus.core.bean.product.ProductQuery;
import com.chocus.core.bean.product.ProductQuery.Criteria;
import com.chocus.core.bean.product.Sku;
import com.chocus.core.dao.product.ProductDao;
import com.chocus.core.dao.product.SkuDao;

import cn.itcast.common.page.Pagination;
import redis.clients.jedis.Jedis;

@Service("productService")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;

	@Autowired
	private SkuDao skuDao;

	@Autowired
	@Qualifier("jedis")
	private Jedis jedis;

	@Autowired
	private JmsTemplate jmsTemplate;

	/**
	 * 获取商品列表
	 * 
	 * @param pageNo
	 * @param name
	 * @param isShow
	 * @param brandId
	 * @return
	 */
	@Override
	public Pagination getProductList(Integer pageNo, String name, Boolean isShow, Long brandId) {

		ProductQuery productQuery = new ProductQuery();
		productQuery.setPageNo(Pagination.cpn(pageNo));
		productQuery.setPageSize(10);

		Criteria criteria = productQuery.createCriteria();
		StringBuilder params = new StringBuilder();

		if (name != null) {
			criteria.andNameLike("%" + name + "%");
			params.append("name=" + name);
		}

		if (isShow != null) {
			criteria.andIsShowEqualTo(isShow);
			params.append("&isShow=" + isShow);
		} else {
			criteria.andIsShowEqualTo(false);
			params.append("&isShow=").append(false);
		}

		if (brandId != null) {
			criteria.andBrandIdEqualTo(brandId);
			params.append("&brandId=" + brandId);
		}

		Pagination pagination = new Pagination(productQuery.getPageNo(), productQuery.getPageSize(),
				productDao.countByExample(productQuery));
		pagination.setList(productDao.selectByExample(productQuery));

		String url = "/product/list.do";
		pagination.pageView(url, params.toString());
		return pagination;
	}

	/**
	 * 保存商品
	 * 
	 * @param product
	 */
	public void insertProduct(Product product) {
		// 保存商品
		Long pno = jedis.incr("pno");
		product.setId(pno);
		product.setIsShow(false);
		product.setIsDel(true);
		product.setCreateTime(new Date());
		productDao.insertSelective(product);
		// 保存库存
		for (String color : product.getColors().split(",")) {
			for (String size : product.getSizes().split(",")) {
				Sku sku = new Sku();
				sku.setProductId(product.getId());
				sku.setColorId(Long.parseLong(color));
				sku.setSize(size);
				sku.setDeliveFee(10f);
				sku.setPrice(100f);
				sku.setStock(999);
				sku.setUpperLimit(200);
				sku.setMarketPrice(200f);
				sku.setCreateTime(new Date());
				skuDao.insertSelective(sku);
			}
		}
	}

	/**
	 * 上架
	 * 
	 * @return
	 */
	public void isShow(Long[] ids) {
		// 改变上架状态
		Product product = new Product();
		product.setIsShow(true);
		for (final Long id : ids) {
			product.setId(id);
			productDao.updateByPrimaryKeySelective(product);
			// 发送消息到mq中
			jmsTemplate.send(new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					return session.createTextMessage(String.valueOf(id));
				}
			});
		}
	}

	/**
	 * 从redis获取所有品牌列表
	 */
	@Override
	public List<Brand> getBrandlistFromRedis() {
		// 从redis中取出品牌列表
		Map<String, String> map = jedis.hgetAll("brand2");
		Set<Entry<String, String>> entrySet = map.entrySet();
		// 存放取出的品牌
		List<Brand> brands = new ArrayList<Brand>();
		// 遍历
		for (Entry<String, String> entry : entrySet) {
			Brand brand = new Brand();
			brand.setId(Long.parseLong(entry.getKey()));
			brand.setName(entry.getValue());
			brands.add(brand);
		}
		return brands;
	}
}
